unit mc_mail;

interface

uses sysutils,classes,mu_parser;


type

TMcMsg=Class(TObject)
private
       ffrom:string;
       fto:array[1..10] of string[200];
       ftext:string;
       furltext:string;
       fdate:string;
       fsubj:string;
       fhas:boolean;
public
      constructor Create;
      destructor Destroy;
      property has:boolean read fhas;
      procedure fillMsg(from:string;subj:string;mes:string;date:string);
      procedure addRep(log:string);
      function repCount:integer;
      function allreps:string;
      property text:string read ftext;
      property utext:string read furltext write furltext;
      property date:string read fdate;
      property subject:string read fsubj;
      property from:string read ffrom;
      procedure getUnparsed(var s:TStrings);
      procedure Clear;
end;

TmcMlist=class(TObject)
private
       fmsg:TmcMsg;
       fst:TStrings;
       fname:string;
       fcount:longint;
       findex,fsize:array[0..1000] of longint;
public
      constructor Create;
      destructor Destroy;
      procedure loadfromFile(name:string);
      property count:longint read fcount;
      procedure getMsg(num:longint; var msg:TmcMsg);
      procedure addMsg(msg:TmcMsg);
      procedure SaveToFile(name:string);
      function getFrom(num:integer):string;
      procedure DelMsg(num:integer);
end;


implementation

procedure TmcMlist.DelMsg;
var
 i:integer;
begin
 if fcount=0 then exit;
 if num>(fcount-1) then exit;
 for i:=1 to fsize[num]-findex[num]+1 do fst.delete(findex[num]);
 move(findex[num+1],findex[num],sizeof(longint)*(fcount-num));
 move(fsize[num+1],fsize[num],sizeof(longint)*(fcount-num));
 findex[fcount-1]:=0;
 fsize[fcount-1]:=0;
 fcount:=fcount-1;
 fst.SaveToFile(fname);
 loadfromfile(fname);
end;

function TmcMlist.getFrom;
begin
 result:='';
 if fcount<num then exit;
 fmsg:=TmcMsg.create;
 getmsg(num,fmsg);
 result:=fmsg.from;
 fmsg.free;
end;
procedure TmcMlist.SaveToFile;
begin
 fst.SaveToFile(name);
end;

procedure TmcMlist.addMsg;
var
 s:Tstrings;
begin
 s:=TStringlist.create;
 msg.getUnparsed(s);
 fst.AddStrings(s);
 fcount:=fcount+1;
 s.free;
end;

procedure TMcMlist.getMsg;
var
 i:integer;
 ind:longint;
 m1,from,who,subj,date,mes:string;
 p:string;
begin
  msg.clear;
  if num>=fcount then exit;
  ind:=findex[num];
  from:=fst[ind];
  delete(from,1,8);
  ind:=ind+1;
  who:=fst[ind];
  delete(who,1,6);
  ind:=ind+1;
  subj:=fst[ind];
  delete(subj,1,11);
  ind:=ind+1;
  date:=fst[ind];
  delete(date,1,8);
  ind:=ind+2;
  mes:='';m1:='';
  for i:=ind to fsize[num]-1 do begin
   mes:=mes+fst[i];
   if mes='' then continue;
   if mes[length(mes)]<>chr($0a) then mes:=mes+chr($0A) else mes:=mes+chr($0a);
  end;
  msg.utext:=mes;
  msg.fillMsg(from,subj,mes,date);
  for i:=1 to 10 do begin
   p:='';
   p:=getToken(who,i,';');
   if p='' then break;
   msg.addRep(p);
  end;
end;

procedure TmcMlist.loadfromFile;
var
 i:longint;
 s,s1:string;
 k:longint;
begin
 fillchar(findex,sizeof(findex),0);
 fillchar(fsize,sizeof(fsize),0);
 if not fileexists(name) then exit;
 fname:=name;
 fst.clear;
 fst.loadfromfile(name);
 if (fst.count=0) or (length(fst[0])=0) or (fst[0][1]<>'#') then exit;
 k:=0;
 for i:=0 to fst.count-1 do begin
  s:=fst[i];
  s1:=copy(s,1,8);
  if s1='##From##' then begin
   findex[k]:=i;
   if k>0 then fsize[k-1]:=i-1;
   k:=k+1;
  end;
 end;
 fcount:=k;
 fsize[k-1]:=fst.count-1;
end;

constructor TmcMlist.Create;
begin
 inherited create;
 fmsg:=TMcMsg.create;
 fcount:=0;
 fname:='';
 fst:=TstringList.create;
end;

destructor TmcMlist.Destroy;
begin
 inherited destroy;
 fmsg.free;
 fst.free;
end;

procedure TmcMsg.getUnparsed;
begin
 if fhas=false then exit;
 s.add('##From##'+ffrom);
 s.add('##To##'+allreps);
 s.Add('##Subject##'+fsubj);
 s.add('##Date##'+fdate);
 s.add('##Message##');
 s.add(ftext);
 s.add('##EndMessage##');
end;

function TmcMsg.allreps;
var
 i:integer;
begin
 result:='';
 for i:=1 to 10 do if fto[i]<>'' then result:=result+fto[i]+';';
end;

function Tmcmsg.repCount;
var
 i:integer;
begin
 result:=0;
 for i:=1 to 10 do if fto[i]='' then begin
  result:=i-1;
  break;
 end;
end;

procedure TmcMsg.clear;
begin
 fhas:=false;
end;

procedure Tmcmsg.addRep(log:string);
var
 i:integer;
begin
 for i:=1 to 10 do if fto[i]='' then begin
  fto[i]:=trim(log);
  break;
 end;
end;

procedure TmcMsg.fillMsg;
begin
 ffrom:=trim(from);
 fsubj:=subj;
 ftext:=mes;
 fdate:=date;
 fhas:=true;
 fillchar(fto,sizeof(fto),0);
end;

constructor TMcMSg.Create;
begin
 inherited create;
 fhas:=false;
 fillchar(fto,sizeof(fto),0);
end;

destructor TmcMsg.Destroy;
begin
 inherited destroy;
end;

end.
